package com.fit.pages;

import java.util.Date;

import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.annotations.SetupRender;
import org.apache.tapestry5.ioc.annotations.Inject;

import com.fit.beans.CarBean;
import com.fit.beans.ServiceBean;
import com.fit.enums.AccountRole;
import com.fit.interfaces.CarI;
import com.fit.interfaces.ServiceI;
import com.fit.interfaces.User;
import com.fit.service.CarsI;
import com.fit.service.ServicesI;
import com.fit.services.AppModule;

public class EntryCarService {

	@SessionState
	private User user;
	
	private boolean userExists;

	public User getUser() {
		return user;
	}
	
	@Persist
	private long serviceCar_id;
	
	@Persist
	private String serviceCar_modelName;
	
	
	
	@Persist
	@Property
	private ServiceI chosenService;
	
	@Inject
	private ServicesI services;
	
	@Inject
	private CarsI cars;
	
	@SetupRender
	public void createObject(){
		chosenService = (ServiceI) AppModule.context.getBean("service");
	}
	
	@Persist
	@Property
	private String description;
	
	@Persist
	@Property
	private Date dateOfService;
	
	@InjectPage
	private BrowseService browseServicePage;
	
	
	Object onActivate(){
		if(!userExists) return Login.class;
		if(user.getRole() == AccountRole.USER) return Index.class;
		return null;
	}
	
	
	
	
	Object onSuccess(){
		CarI currentCar = new CarBean();
		currentCar = cars.retriveSingleCar(getServiceCar_id());
		currentCar.setServiceDone(true);
		chosenService.executeServiceCreation(description, dateOfService, currentCar);
		services.save(chosenService);
//		cars.deleteSingleCar(getServiceCar_id());
		
		description = "";
		dateOfService = null;
		currentCar = null;
		return browseServicePage;
		
	}


	public void setServiceCar_id(long serviceCar_id) {
		this.serviceCar_id = serviceCar_id;
	}


	public long getServiceCar_id() {
		return serviceCar_id;
	}




	public void setServiceCar_modelName(String serviceCar_modelName) {
		this.serviceCar_modelName = serviceCar_modelName;
	}




	public String getServiceCar_modelName() {
		return serviceCar_modelName;
	}
	
}
